package com.smart.community.region.controller;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smart.community.commons.annotation.ApiLog;
import com.smart.community.commons.enums.ApiLogOperationType;
import com.smart.community.commons.result.PageResult;
import com.smart.community.commons.result.Result;
import com.smart.community.region.dto.OwnerHouseholdDTO;
import com.smart.community.region.dto.OwnerHouseholdQueryDTO;
import com.smart.community.region.service.IOwnerHouseholdService;
import com.smart.community.region.vo.OwnerHouseholdVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * 业主房户关联控制器
 * 
 * @author Wu.Liang
 * @since 2025-08-20
 * @version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/region/owner/household")
@Tag(name = "业主房户关联管理", description = "业主房户关联相关接口")
@Validated
public class OwnerHouseholdController {
    
    @Autowired
    private IOwnerHouseholdService ownerHouseholdService;

    /**
     * 获取业主关联的房户列表
     * 
     * @param ownerId 业主ID
     * @return 房户关联列表
     * @throws Exception 统一异常
     */
    @GetMapping("/owner/{ownerId}")
    @Operation(summary = "获取业主关联的房户列表", description = "根据业主ID获取关联的房户列表")
    public Result<List<OwnerHouseholdVO>> getOwnerHouseholds(
            @Parameter(description = "业主ID") @PathVariable @NotNull(message = "业主ID不能为空") Long ownerId) throws Exception {
        log.info("获取业主关联的房户列表，业主ID：{}", ownerId);
        
        List<OwnerHouseholdVO> result = ownerHouseholdService.getOwnerHouseholdVOs(ownerId);
        
        log.info("获取业主关联的房户列表成功，业主ID：{}，关联数量：{}", ownerId, result.size());
        return Result.success("查询成功", result);
    }

    /**
     * 获取房户关联的业主列表
     * 
     * @param householdId 房户ID
     * @return 业主关联列表
     * @throws Exception 统一异常
     */
    @GetMapping("/household/{householdId}")
    @Operation(summary = "获取房户关联的业主列表", description = "根据房户ID获取关联的业主列表")
    public Result<List<OwnerHouseholdVO>> getHouseholdOwners(
            @Parameter(description = "房户ID") @PathVariable @NotNull(message = "房户ID不能为空") Long householdId) throws Exception {
        log.info("获取房户关联的业主列表，房户ID：{}", householdId);
        
        List<OwnerHouseholdVO> result = ownerHouseholdService.getHouseholdOwnerVOs(householdId);
        
        log.info("获取房户关联的业主列表成功，房户ID：{}，关联数量：{}", householdId, result.size());
        return Result.success("查询成功", result);
    }

    /**
     * 创建业主房户关联
     * 
     * @param dto 关联信息
     * @return 关联ID
     * @throws Exception 统一异常
     */
    @PostMapping
    @Operation(summary = "创建业主房户关联", description = "创建业主与房户的关联关系")
    @ApiLog(logTitle = "业主房户关联", logType = 1, moduleName = "区域管理", operationType = ApiLogOperationType.ADD)
    public Result<Long> createOwnerHousehold(
            @Parameter(description = "关联信息") @RequestBody @Valid OwnerHouseholdDTO dto) throws Exception {
        log.info("创建业主房户关联，业主ID：{}，房户ID：{}", dto.getOwnerId(), dto.getHouseholdId());
        
        Long result = ownerHouseholdService.createOwnerHousehold(dto);
        
        log.info("创建业主房户关联成功，关联ID：{}", result);
        return Result.success("创建成功", result);
    }

    /**
     * 更新业主房户关联
     * 
     * @param id 关联ID
     * @param dto 关联信息
     * @return 操作结果
     * @throws Exception 统一异常
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新业主房户关联", description = "更新业主与房户的关联关系")
    @ApiLog(logTitle = "业主房户关联", logType = 1, moduleName = "区域管理", operationType = ApiLogOperationType.UPDATE)
    public Result<Void> updateOwnerHousehold(
            @Parameter(description = "关联ID") @PathVariable @NotNull(message = "关联ID不能为空") Long id,
            @Parameter(description = "关联信息") @RequestBody @Valid OwnerHouseholdDTO dto) throws Exception {
        log.info("更新业主房户关联，关联ID：{}", id);
        log.info("接收到的DTO数据 - mortgageInfo: {}, remark: {}", dto.getMortgageInfo(), dto.getRemark());
        
        ownerHouseholdService.updateOwnerHousehold(id, dto);
        
        log.info("更新业主房户关联成功，关联ID：{}", id);
        return Result.success("更新成功", null);
    }

    /**
     * 删除业主房户关联
     * 
     * @param id 关联ID
     * @return 操作结果
     * @throws Exception 统一异常
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除业主房户关联", description = "删除业主与房户的关联关系")
    @ApiLog(logTitle = "业主房户关联", logType = 1, moduleName = "区域管理", operationType = ApiLogOperationType.DELETE)
    public Result<Void> deleteOwnerHousehold(
            @Parameter(description = "关联ID") @PathVariable @NotNull(message = "关联ID不能为空") Long id) throws Exception {
        log.info("删除业主房户关联，关联ID：{}", id);
        
        ownerHouseholdService.deleteOwnerHousehold(id);
        
        log.info("删除业主房户关联成功，关联ID：{}", id);
        return Result.success("删除成功", null);
    }

    /**
     * 设置主业主
     * 
     * @param ownerId 业主ID
     * @param householdId 房户ID
     * @return 操作结果
     * @throws Exception 统一异常
     */
    @PutMapping("/owner/{ownerId}/household/{householdId}/primary")
    @Operation(summary = "设置主业主", description = "设置指定业主为房户的主业主")
    @ApiLog(logTitle = "设置主业主", logType = 1, moduleName = "区域管理", operationType = ApiLogOperationType.UPDATE)
    public Result<Void> setPrimaryOwner(
            @Parameter(description = "业主ID") @PathVariable @NotNull(message = "业主ID不能为空") Long ownerId,
            @Parameter(description = "房户ID") @PathVariable @NotNull(message = "房户ID不能为空") Long householdId) throws Exception {
        log.info("设置主业主，业主ID：{}，房户ID：{}", ownerId, householdId);
        
        ownerHouseholdService.setPrimaryOwner(ownerId, householdId);
        
        log.info("设置主业主成功，业主ID：{}，房户ID：{}", ownerId, householdId);
        return Result.success("设置成功", null);
    }

    /**
     * 分页查询业主房户关联列表
     * 
     * @param queryDTO 分页查询条件
     * @return 分页结果
     * @throws Exception 统一异常
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询业主房户关联列表", description = "分页查询业主房户关联信息")
    public Result<PageResult<OwnerHouseholdVO>> getOwnerHouseholdPage(@Valid OwnerHouseholdQueryDTO queryDTO) throws Exception {
        log.info("分页查询业主房户关联列表，查询条件：{}", queryDTO);
        
        PageResult<OwnerHouseholdVO> result = ownerHouseholdService.getOwnerHouseholdPage(queryDTO);
        
        log.info("分页查询业主房户关联列表成功，总记录数：{}", result.getTotal());
        return Result.success("查询成功", result);
    }

    /**
     * 根据ID获取业主房户关联详情
     * 
     * @param id 关联ID
     * @return 关联详情
     * @throws Exception 统一异常
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取业主房户关联详情", description = "根据关联ID获取详细信息")
    public Result<OwnerHouseholdVO> getOwnerHouseholdById(
            @Parameter(description = "关联ID") @PathVariable @NotNull(message = "关联ID不能为空") Long id) throws Exception {
        log.info("获取业主房户关联详情，关联ID：{}", id);
        
        OwnerHouseholdVO result = ownerHouseholdService.getOwnerHouseholdVOById(id);
        
        log.info("获取业主房户关联详情成功，关联ID：{}", id);
        return Result.success("查询成功", result);
    }
}
